# Maintainer: Antoine Martin <totaam@xpra.org>

_realname=PyOpenGL
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}"  "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=3.1.1a1
pkgrel=3
pkgdesc="PyOpenGL is the most common cross platform Python binding to OpenGL and related APIs (mingw-w64)"
arch=('any')
url='http://pyopengl.sourceforge.net'
license=('PyOpenGL')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-cython2"
             "${MINGW_PACKAGE_PREFIX}-cython"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-setuptools")
options=('staticlibs' 'strip' '!debug')
_dtoken='df/fe/b9da75e85bcf802ed5ef92a5c5e4022bf06faa1d41b9630b9bb49f827483'
_acceltoken='84/74/b48e413c97cbe51d778cba58a431bb8348d826a1576ab48b3c5340628bd8'
source=("https://files.pythonhosted.org/packages/${_dtoken}/${_realname}-${pkgver}.tar.gz"
        "https://files.pythonhosted.org/packages/${_acceltoken}/${_realname}-accelerate-${pkgver}.tar.gz"
        "formathandler.pxd"
        "wrapper.pxd")
sha512sums=('8f4f57b153ee014dc238fe83180cc92f8e789a274fc6b0cbef6f5dec9dbc44cb1ae1f6b142a6c2b4c8c000709338d64f5968939eeb6a2384f87fb63ae99b40d8'
            '6881e3b435da5a33ff31c0991f403166236b44a5bd21ca60418d131dafc1159d6b1c73e60b935d1761d4fc848b824422abbe0e850fe860fa694b64536dddc35c'
            'c7d846aaf9b3af526ae8870d9821846ddbfb132b4db05a974f0198dea9bf76684f4473bb6ba88e0c6dd3732865b1963489a506dca91d0613b53af598e4fdb684'
            'cc8c06550fb365929ca0fae237298ad64c2d4c14fda8963ab0601bd55c9b43c15787bd77784419d8590335fee4be73b916a19436a752f33ceedc7d6ab3e48c9e')

prepare() {
  #force regeneration of the .c files since the ones distributed
  #with the source are too old and don't compile with python3.7:
  accel_dir="${srcdir}/${_realname}-accelerate-${pkgver}"
  rm -f ${accel_dir}/src/*.c
  #now add the missing sources files:
  #https://github.com/mcfletch/pyopengl/issues/12
  #we have to create a module structure, so cython will find them:
  mkdir ${accel_dir}/OpenGL_accelerate
  cp formathandler.pxd wrapper.pxd "${accel_dir}/OpenGL_accelerate/"
  cp ${accel_dir}/__init__.py "${accel_dir}/OpenGL_accelerate/"

  cd "${srcdir}"
  for builddir in python{2,3}-build-${CARCH}; do
    rm -rf ${builddir} | true
    cp -r "${_realname}-${pkgver}" "${builddir}"
    cp -r "${_realname}-accelerate-${pkgver}" "${builddir}-accelerate"
  done
  #'async' is a keyword now in python 3.7,
  #just remove those extensions which aren't used on win32 anyway:
  rm -fr python3-build-${CARCH}/OpenGL/GL/SGIX/async*
  rm -fr python3-build-${CARCH}/OpenGL/raw/GL/SGIX/async*
}

build() {
  for pver in {2,3}; do  
    msg "Python ${pver} build for ${CARCH}"  
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
    msg "accelerate module:"  
    cd "${srcdir}/python${pver}-build-${CARCH}-accelerate"
    ${MINGW_PREFIX}/bin/python${pver} setup.py build
  done  
}

check() {
  for pver in {2,3}; do
    msg "Python ${pver} test for ${CARCH}"
    cd "${srcdir}/python${pver}-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python${pver} setup.py check
    cd "${srcdir}/python${pver}-build-${CARCH}-accelerate"
    ${MINGW_PREFIX}/bin/python${pver} setup.py check
  done
}

package_python3-PyOpenGL() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  cd "${srcdir}/python3-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 license.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING"

  cd "${srcdir}/python3-build-${CARCH}-accelerate"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
}

package_python2-PyOpenGL() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")

  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build

  install -Dm644 license.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING"

  cd "${srcdir}/python2-build-${CARCH}-accelerate"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1 --skip-build
}

package_mingw-w64-i686-python2-PyOpenGL() {
  package_python2-PyOpenGL
}

package_mingw-w64-i686-python3-PyOpenGL() {
  package_python3-PyOpenGL
}

package_mingw-w64-x86_64-python2-PyOpenGL() {
  package_python2-PyOpenGL
}

package_mingw-w64-x86_64-python3-PyOpenGL() {
  package_python3-PyOpenGL
}
